<template>
  <div class="loginall">
    <div class="loginbox">
      <el-form 
      :model="loginForm" 
      label-width="0px" 
      class="loginform"
      :rules="loginrules"
      ref="loginFormRef"
      >
        
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-a-gerenyonghu"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-a-suosimiyinsi"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>

        <el-form-item class="btns">
          <el-button plain @click="login">登录</el-button>
          <el-button plain>取消</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>
export default {
  name:"Login",
  data() {
    return {
      loginForm: {
        username: "wenrouyue",
        password: "123123",
        time :new  Date().getTime(),
      },
      loginrules:{
        username:[{ required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }],
        password:[{ required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }]
      },
    };
  },
  methods: {
    login(){
        this.$refs.loginFormRef.validate(async (vaild)=>{

           if (!vaild) return;
            const {data:res} = await this.$axios.post('/user/login',this.loginForm);
            if (res.code !== 200) {
              this.$message.error("账号或密码错误！")
              return
            }
            window.sessionStorage.setItem("token",(this.$md5(String(this.loginForm.time * 1547788176 - 743123321))))
            this.$message.success("登录成功！")
            this.$router.push({
              name:'index',
              params: { token: (this.$md5(String(this.loginForm.time * 1547788176 - 743123321))) }
            })
        })
    }
  },
};
</script>

<style scoped>
.loginall {
  background-color: aqua;
  height: 100%;
}
.loginbox {
  width: 450px;
  height: 300px;
  background-color: azure;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.loginform {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>

